package com.hnkywl.sts.dao.sfgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.sfgl.Sfk;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sfgl
 * 类描述:    付款审核dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class TpfkDao extends SimpleHibernateTemplate<Sfk, Long> {
    /**
     * 查询付款审核
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Sfk> findTpfkList(String bm, String sbgsMc, Long nbgsId,String gldwMc, String zt,String zdr, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfk> page = new Page<Sfk>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (bm != null && !bm.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" bm like ? ");
            param.add("%" + bm.trim() + "%");
        }
        if (zdr != null && !zdr.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" createUser.xm like ? ");
            param.add("%" + zdr.trim() + "%");
        }
        if (gldwMc != null && !gldwMc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gldw.qc like ? ");
            param.add("%" + gldwMc.trim() + "%");
        }
        if (nbgsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sbgs.id = ? ");
            param.add(nbgsId);
        }
        if (nbgsId == null && sbgsMc != null && !sbgsMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sbgs.jc like ? ");
            param.add("%" + sbgsMc.trim() + "%");
        }

        if (zt != null && !zt.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zt = ? ");
            param.add(zt);
        }
        //==============记录数量===============
        hql = "select count(*) from Sfk where sfklx in (1,2) and sftpfy = 'tpfk' "  + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfk where sfklx in (1,2) and sftpfy = 'tpfk' "  + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    /**
     * 按照采购合同查询付款审核
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Sfk> findTpfkBySfkList( Long sfkId, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfk> page = new Page<Sfk>(pageNo, pageSize, 0);

        if (sfkId != null ) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" id = ? ");
            param.add(sfkId);
        }
        //==============记录数量===============
        hql = "select count(*) from Sfk where sfklx in (1,2)" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfk where sfklx in (1,2)" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    public Sfk getTpfk(Long sfkId){
        String hql = "from Sfk where sfklx in (1,2) and id = ?";
        return   (Sfk)find(hql,sfkId).get(0);
    }

    /**
     * 判断属性是否重复
     * @param id 付款审核id
     * @param propertyName 付款审核属性名
     * @param propertyValue 付款审核属性值
     * @return
     */
    public Boolean checkUnique(Long id, String propertyName,Object propertyValue) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Sfk where sfklx in (1,2) and " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Sfk where sfklx in (1,2) and " + propertyName + " = ? and id <>?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }
}
